package io.geekidea.boot.outboundBenhavior.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.protobuf.Api;
import io.geekidea.boot.auth.util.LoginUtil;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.outboundBenhavior.dto.OutboundBenhaviorDto;
import io.geekidea.boot.outboundBenhavior.entity.OutboundBehavior;
import io.geekidea.boot.outboundBenhavior.mapper.OutboundBenhaviorMapper;
import io.geekidea.boot.outboundBenhavior.query.OutboundBenhaviorQuery;
import io.geekidea.boot.outboundBenhavior.service.OutboundBenhaviorService;
import io.geekidea.boot.outboundBenhavior.vo.OutboundBenhaviorVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出库行为控制器
 */
@Slf4j
@RestController
@Tag(name = "出库行为")
public class OutboundBenhaviorController {

    @Resource
    private OutboundBenhaviorService outboundBenhaviorService;

    @Resource
    private OutboundBenhaviorMapper outboundBenhaviorMapper;


    @PostMapping("/Ben/list")
    @Operation(summary = "出库行为列表", description = "按照多条件组合分页查询")
    public ApiResult outBenList(
            @RequestBody OutboundBenhaviorQuery outboundBenhaviorQuery) {
        List<OutboundBehavior> outBehList = outboundBenhaviorService.getOutBehList(outboundBenhaviorQuery);
        int total = outboundBenhaviorService.getTotal(outboundBenhaviorQuery).getTotal();
        PageDto<OutboundBehavior> pageDto = new PageDto<>(outBehList,total,outboundBenhaviorQuery.getPageSize(),outboundBenhaviorQuery.getPageIndex()+1);
        Paging<OutboundBehavior> paging = new Paging<>(outBehList);
        return ApiResult.success(pageDto);
    }

    @Operation(summary = "根据id查询详情")
    @GetMapping("/getOutBeh/{id}")
    public ApiResult getOutBhe(@PathVariable(value = "id") Integer id){
        OutboundBehavior outboundBehavior = outboundBenhaviorMapper.getOutBhe(id);
        return ApiResult.success(outboundBehavior);
    }


    @Operation(summary = "添加出库行为")
    @PostMapping("/addOutBeh")
    public ApiResult addOutBeh(@RequestBody OutboundBenhaviorDto outboundBenhaviorDto) throws Exception {
        outboundBenhaviorDto.setCreatedBy(LoginUtil.getUsername());
        boolean b = outboundBenhaviorService.addOutBeh(outboundBenhaviorDto);
        if (!b){
            return ApiResult.success("出库行为添加成功");
        }
        return ApiResult.success("出库行为添加成功");
    }


    @PostMapping("/updateOutbenh")
    @Operation(summary = "修改人员管理",description = "修改人员")
    public ApiResult updateOutbeh(@RequestBody OutboundBehavior outboundBehavior) throws Exception {
        outboundBehavior.setUpdatedBy(LoginUtil.getUsername());
        boolean b = outboundBenhaviorService.updateOutbeh(outboundBehavior);
        if (!b){
            return ApiResult.success("出库行为添加成功");
        }
        return ApiResult.success("出库行为添加成功");
    }
}
